package com.huobao.yunying.activitys_log_reg;

import java.util.Map;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.services.RegTimeService;
import com.huobao.yunying.services.RegTimeUtils;
import com.huobao.yunying.utils.Utils;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.dialog.BaseDialog;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Act_FindPwd extends HuobaoBaseActivity implements OnClickListener{

	@ViewInject(R.id.btn_sendyanzhengma)
	private Button btn_yanzheng;
	@ViewInject(R.id.edt_shoujihao)
	private EditText edt_shoujihao;
	@ViewInject(R.id.edt_yanzhengma)
	private EditText edt_yanzhengma;
	@ViewInject(R.id.tv_login)
	private TextView tv_login;
	@Override
	protected int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.layout_findpwd;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return "手机找回密码";
	}

	@Override
	protected void init() {
		tv_login.setOnClickListener(this);
		EventBus.getDefault().register(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
	@Subscriber(tag = "regTime")
	public void onReceiverTime(int time) {
		if (time < 60) {
			btn_yanzheng.setEnabled(false);
			btn_yanzheng.setText((60 - time) + "秒后重新获取");
		} else {
			RegTimeUtils.clear(this);
			btn_yanzheng.setEnabled(true);
			btn_yanzheng.setText("发送验证码");
		}
	}
	
	public void onSendYnazheng(View view){
		String num = edt_shoujihao.getText().toString().trim();
		if(Utils.isMobileNO(num)){
			sendYanzheng(num);
		}else{
			Toast.makeText(context, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
		}
	}
	public void onFindClick(View view){
		String phone_mob = edt_shoujihao.getText().toString().trim();
		String confirm_code = edt_yanzhengma.getText().toString().trim();
		if(TextUtils.isEmpty(phone_mob) || Utils.isMobileNO(phone_mob) == false){
			Toast.makeText(context, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
			return;
		}
		if(TextUtils.isEmpty(confirm_code)){
			Toast.makeText(context, "请输入验证码", Toast.LENGTH_SHORT).show();
			return;
		}
		Map<String, String> map = HttpUtil.getParams("app","findpwd","phone_mob",phone_mob,"confirm_code",confirm_code);
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				String id= jsonObject.getString("id");
				String activation= jsonObject.getString("activation");
				check(id, activation);
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	protected void check(String id, String activation) {
		Map<String, String> map = HttpUtil.getParams("app","findpwd","act","set_password","id",id,"activation",activation);
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				String id= jsonObject.getString("id");
				String activation= jsonObject.getString("activation");
				Intent intent = new Intent(context, Act_SetNewPwd.class);
				intent.putExtra("id", id);
				intent.putExtra("activation", activation);
				startActivity(intent);
				finish();
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private void sendYanzheng(String phone) {
		Map<String, String> map = HttpUtil.getParams("app", "sms", "act", "send_code","phone_mob",phone);
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				int retval = jsonObject.getIntValue("msg");
				String msg = jsonObject.getString("retval");
				//（1手机号不正确）,（2此手机号尚未注册）,(3短信发送失败) (4短信发送成功)
				switch (retval) {
				case 1:
				case 2:
				case 3:
					BaseDialog.showAlert(msg, context);
					break;
				case 4:
					Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
					startService(new Intent(context, RegTimeService.class));
					break;
				}
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.tv_login:
			Intent intent = new Intent(this, Act_Login.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  
			startActivity(intent);
			finish();
			break;
		}
	}
}
